home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Graphics / Misc / BackBounce / Source / Animator.m < prev    next >
Encoding:
Text File  |  1995-06-12  |  4.3 KB  |  220 lines

  1.  
  2. /* Not generated by Interface Builder */
  3.  
  4. #import "Animator.h"
  5. #import <appkit/Control.h>
  6. #import <appkit/Button.h>
  7. #import <sys/time.h>
  8.  
  9. @implementation Animator
  10.  
  11. // These are our default values for a new Animator.
  12. static float defaultTiming, defaultThreshold;
  13. static SEL defaultAction;
  14. BOOL defaultRunning;
  15.  
  16. + initialize
  17. {
  18.   [self setDefaultTiming:0.0];
  19.   [self setDefaultThreshold:NX_BASETHRESHOLD];
  20.   [self setDefaultAction:@selector( animate:)];
  21.   [self setDefaultRunning:NO];
  22.   return self;
  23. }
  24.  
  25. // Where our DPSTimedEntry ends up.  data should be self, so that the correct
  26. // Animator instance can be called to sendAction.
  27. static void doAnimator( DPSTimedEntry te, double curTime, void *data)
  28. {
  29.     [(id)data sendAction];
  30. }
  31.  
  32. + new                // create a new Animator.
  33. {
  34.     self = [super new];
  35.     target=nil;
  36.     [self setTiming:defaultTiming];
  37.     [self setThreshold:defaultThreshold];
  38.     running=NO;
  39.     [self setRunning:defaultRunning];
  40.     [self setAction:defaultAction];
  41.     startTime=elapsed=-1.0;
  42.     return self;
  43. }
  44. + setDefaultTiming:(float)dtiming
  45. {
  46.   defaultTiming=dtiming;
  47.   return self;
  48. }
  49. + setDefaultThreshold:(float)dthreshold
  50. {
  51.   defaultThreshold=dthreshold;
  52.   return self;
  53. }
  54. + setDefaultAction:(SEL)daction
  55. {
  56.   defaultAction=daction;
  57.   return self;
  58. }
  59. + setDefaultRunning:(BOOL)state
  60. {
  61.   defaultRunning=state;
  62.   return self;
  63. }
  64.  
  65. - free
  66. {
  67.     [self stop:self];
  68.     return [super free];
  69. }
  70.  
  71. - setTarget:anObject
  72. {
  73.     if( target==anObject)    // if already set,
  74.       return self;        // just return.
  75.     target = anObject;
  76.     if( target && [anObject respondsTo:@selector( setAnimator:)])
  77.       [anObject setAnimator:self];    // anObject can store us,
  78.                     // or just set us up.
  79.     return self;
  80. }
  81. - target { return target; }
  82.  
  83. - setTiming:(float)t
  84. {
  85.   timing=t;
  86.   if( running)
  87.     [[self stop:self] start:self];
  88.   return self;
  89. }
  90. -(float)timing { return timing; }
  91.  
  92. - setThreshold:(float)t
  93. {
  94.   threshold=t;
  95.   if( running)
  96.     [[self stop:self] start:self];
  97.   return self;
  98. }
  99. -(float)threshold { return threshold; }
  100.  
  101. -(double)doubleValue        // return the amount of time since last call.
  102. {
  103.   return running ? elapsed : -1.0;
  104. }
  105. -(float)floatValue        // return as a float.
  106. {
  107.   return [self doubleValue];
  108. }
  109. -(int)intValue
  110. {
  111.   return [self doubleValue];
  112. }
  113.  
  114. - setAction:(SEL)theAction
  115. {
  116.   action=theAction;
  117.   return self;
  118. }
  119. -(SEL)action { return action; }
  120.  
  121. - setRunning:(BOOL)state
  122. {
  123.   return state==running ? self : [self toggleRun:self];
  124. }
  125. - (BOOL)running    { return running; }
  126.  
  127. - sendAction:(SEL)theAction to:theTarget
  128. {
  129.   struct timeval curTime;
  130.   double now;
  131.   gettimeofday( &curTime, 0);
  132.   now=(float)(curTime.tv_sec)+(float)(curTime.tv_usec)/1000000.0;
  133.   elapsed=now-startTime;
  134.   if( theAction && theTarget && [theTarget respondsTo:theAction])
  135.     [theTarget perform:theAction with:self];
  136.   return self;
  137. }
  138. - sendAction
  139. {
  140.   return [self sendAction:action to:target];
  141. }
  142.  
  143. - resetValue:(double)value
  144. {
  145.   startTime=value;
  146.   elapsed=0;
  147.   return self;
  148. }
  149. - resetValue
  150. {
  151.     struct timeval curTime;
  152.     gettimeofday( &curTime, 0);
  153.     return [self resetValue:(float)(curTime.tv_sec)+(float)(curTime.tv_usec)/1000000.0];
  154. }
  155. - start:sender
  156. {
  157.     if( !running)
  158.       te=DPSAddTimedEntry( timing, &doAnimator, self, threshold);
  159.     running=YES;
  160.     return [self resetValue];
  161. }
  162. - stop:sender
  163. {
  164.     if( running)
  165.       DPSRemoveTimedEntry( te);
  166.     running=NO;
  167.     return self;
  168. }
  169. - toggleRun:sender
  170. {
  171.   return running ? [self stop:sender] : [self start:sender];
  172. }
  173. - takeTimingFrom:sender
  174. {
  175.   [self setTiming:[sender floatValue]];
  176.   return self;
  177. }
  178. - takeRunningFrom:sender
  179. {
  180.   [self setRunning:[sender state]];
  181.   return self;
  182. }
  183.  
  184. - copy
  185. {
  186.   id newAnimator=[super copy];
  187.   [newAnimator setTarget:nil];
  188.   [newAnimator setTiming:[self timing]];
  189.   [newAnimator setThreshold:[self threshold]];
  190.   [newAnimator setAction:[self action]];
  191.   [newAnimator setRunning:[self running]];
  192.   return newAnimator;
  193. }
  194.  
  195. - awake
  196. {
  197.     if( running)
  198.       {
  199.         running=NO;
  200.         [self start:self];
  201.       }
  202.     return self;
  203. }
  204. - write:(NXTypedStream *)stream
  205. {
  206.     [super write:stream];
  207.     NXWriteObjectReference( stream, target);
  208.     NXWriteTypes(stream, "ffi:", &timing, &threshold, &running, &action);
  209.     return self;
  210. }
  211. - read:(NXTypedStream *)stream
  212. {
  213.     [super read:stream];
  214.     target=NXReadObject( stream);
  215.     NXReadTypes(stream, "ffi:", &timing, &threshold, &running, &action);
  216.     return self;
  217. }
  218.  
  219. @end
  220.